home *** CD-ROM | disk | FTP | other *** search
- #include <sys/param.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <sys/time.h>
- #include <errno.h>
- #include <ctype.h>
- #include <setjmp.h>
-
- #include "nameipacct.h"
- /* 1.3 90/06/20 dfk@cwi.nl */
-
- #define MAXRC 200 /* length reverse cache */
- #define DEFTOUTS 15
-
- jmp_buf env;
- int timeout ();
- unsigned touts = 0;
-
- extern char *netname();
-
- extern char *optarg;
- extern int optind;
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- register int i;
- char from[16];
- char to[16];
- char l[80];
- int bytes;
- int packets;
-
- while ((i=getopt(argc, argv, "t:")) != EOF)
- {
- switch(i)
- {
- case 't':
- touts = atoi(optarg);
- break;
-
- case '?':
- fprintf(stderr, "usage: nameipacct [-t secs]\n");
- exit(1);
- }
- }
-
- if (touts == 0)
- touts = DEFTOUTS;
- signal(SIGALRM, timeout);
-
- while (fgets(l, sizeof l, stdin) != NULL)
- {
- if (isdigit(l[0]))
- {
- sscanf(l, "%s %s %d %d\n", from, to, &packets, &bytes);
- display(from, to, packets, bytes);
- }
- else
- fputs(l, stdout);
- }
- }
-
- display(froma, toa, packets, bytes)
- char *froma;
- char *toa;
- int packets;
- int bytes;
- {
- char from[40];
- char to[40];
- struct in_addr ha;
- struct hostent *h;
- register int len;
-
- ha.s_addr = inet_addr(froma);
-
- alarm(touts);
- if (!setjmp(env) && !rchit(&ha) && (h=gethostbyaddr(&ha, 4, AF_INET)) != NULL)
- {
- len = strlen(h->h_name);
- strncpy(from, len>29 ? h->h_name+(len-29) : h->h_name, sizeof(from));
- }
- else
- {
- rcenter(&ha);
- sprintf(from, "%s", netname(ha));
- }
- alarm(0);
-
- ha.s_addr = inet_addr(toa);
-
- alarm(touts);
- if (!setjmp(env) && !rchit(&ha) && (h=gethostbyaddr(&ha, 4, AF_INET)) != NULL)
- {
- len = strlen(h->h_name);
- strncpy(to, len>29 ? h->h_name+(len-29) : h->h_name, sizeof(to));
- }
- else
- {
- rcenter(&ha);
- sprintf(to, "%s", netname(ha));
- }
- alarm(0);
-
- /* print data */
- printf("%-29.29s %-29.29s%8d%10d K\n", from, to, packets, bytes/1024);
- }
-
- static unsigned rccurent = 0;
- static unsigned rc[MAXRC];
-
- rcenter(a)
- struct in_addr *a;
- {
- if (rccurent < MAXRC)
- rc[rccurent++] = (unsigned) a->s_addr;
- }
-
- rchit(a)
- struct in_addr *a;
- {
- register int i;
- register unsigned aa;
-
- aa = (unsigned) a->s_addr;
- for (i=0; i<rccurent; i++)
- {
- if (rc[i] == aa)
- return(1);
- }
-
- return(0);
- }
-
- timeout ()
- {
-
- alarm(0);
- longjmp(env, 1);
- }
-
-
-
- char *
- netname(ha)
- struct in_addr ha;
- {
- static char cmd[80];
- static char buf[80];
- static char quadname[16];
- register FILE *pipe;
- register char *p;
-
- strncpy(quadname, inet_ntoa(ha), sizeof quadname);
- #ifdef RIPEDBCMD
- sprintf(cmd, "%s %s", RIPEDBCMD, inet_ntoa(inet_makeaddr(inet_netof(ha),0)));
- if ((pipe=popen(cmd, "r")) == NULL)
- return(quadname);
-
- while (fgets(buf, sizeof buf, pipe) != NULL)
- {
- if (strncmp(buf, "*na: ", 5) == 0)
- {
- for (p=buf+strlen(buf)-1; isspace(*p); p--)
- *p = 0;
- sprintf(cmd, "%s-%s", buf+5, quadname);
- return(cmd);
- }
- }
- #else RIPEDBCMD
- return(quadname);
- #endif RIPEDBCMD
- }
-
-